package acwing._2LanQiao._8NumberTheory;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._2LanQiao._8numberTheory
 * @File : Q1299.java
 * @Author : WangRuoyu
 * @Date : 2023/3/22 10:46
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Q1299 {

    static long exgcd(long a, long b, long[] x, long[] y) {
        if (b == 0) {
            x[0] = 1;
            y[0] = 0;
            return a;
        } else {
            long d = exgcd(b, a % b, y, x);
            y[0] -= a / b * x[0];
            return d;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");
        int t = Integer.parseInt(words[0]);

        for (int i = 0; i < t; ++i) {
            words = br.readLine().split(" ");
            long n = Integer.parseInt(words[0]);
            long d = Integer.parseInt(words[1]);
            long x = Integer.parseInt(words[2]);
            long y = Integer.parseInt(words[3]);

            long dis = (n + y - x) % n;
            long[] a = {0};
            long[] b = {0};
            long dd = exgcd(n, d, b, a);

            if ((dis % dd) != 0) {
                System.out.println("Impossible");
            } else {
                long n_ = n / dd;
                a[0] = ((dis / dd) * a[0]) % n_;
                if (a[0] < 0) {
                    a[0] += n_;
                }
                System.out.println(a[0]);
            }
        }
    }
}